﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using COM.OA.Entity;
using System.Collections.Generic;
using COM.OA.BLL;

public partial class gw_gw_urge : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //联动
    protected void ddldepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        //取部门的值
        string ddldepartment = this.ddldepartment.Text;

        SqlParameter parambumen = new SqlParameter("@deptid", ddldepartment);
        IList<liandongren> lists = BLLGeneric.Select<liandongren>(CommandType.StoredProcedure, "liandong", parambumen);
        this.ddlname.DataSource = lists;
        this.ddlname.DataTextField = "Em_name";
        this.ddlname.DataValueField = "Em_name";
        this.ddlname.DataBind();
    }
    //发送按钮
    protected void btnSend_Click(object sender, EventArgs e)
    {
        //接收人
        string takeuser = this.Request.Form["sendto"];

        //判断接收人是否为空
        if (takeuser == null || takeuser == "")
        {
            this.Response.Write(string.Format(GetRedirect.WINALERT, "接收人不能为空"));
            return;
        }
        //标题
        string title = this.Request.Form["mtitle"];

        //判断标题是否为空
        if (title == null || title == "")
        {
            this.Response.Write(string.Format(GetRedirect.WINALERT, "请输入标题！！"));
            return;
        }

        //内容
        string content = this.FreeTextBox1.Text;

        //判断内容是否为空
        if (content == null || content == "")
        {
            this.Response.Write(string.Format(GetRedirect.WINALERT, "请输入催办内容！！"));
            return;
        }
       
        //接收人id
        int takeuserid = 0;

        string sql = "em_name='{0}'";
        sql = string.Format(sql, takeuser);
        IList<employee> elist = employeeBLL.Select(sql);
        if (elist.Count == 1)
        {
            foreach (employee em in elist)
            {
                takeuserid = em.em_id;
            }
            string uid = "u_em_id={0}";
            uid = string.Format(uid, takeuserid);
            IList<users> u = usersBLL.Select(uid);
            if (u.Count == 1)
            {
                foreach (users user in u)
                {
                    takeuserid = user.u_id;
                }
            }
        }

        //发送人
        string username = this.Request.Form["username"];

        //发送人id
        int userid = 0;

        string sql3 = "u_username='{0}'";
        sql3 = string.Format(sql3, username);
        IList<users> userlist = usersBLL.Select(sql3);
        if (userlist.Count == 1)
        {
            foreach (users u in userlist)
            {
                userid = u.u_id;
            }
        }

        //发布时间
        DateTime time = DateTime.Parse(this.Request.Form["txtsendtime"]);

        //是否回执
        int isreturn = 0;

        //群id
        int gid = 1;

        message mess = new message();
        mess.m_title = title;//标题
        mess.m_content = content;//内容
        mess.m_u_id = takeuserid;//接收人id
        mess.m_sendu_id = userid;//发送人id 
        mess.m_g_id = gid;//群组id
        mess.m_releasetime = time;//发送时间
        mess.m_isreturn = isreturn;//是否回执

        int flag = messageBLL.Insert(mess);
        if (flag == 0)
        {
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('短消息发送失败！！');</script>");
        }
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('短消息发送成功！！');</script>");
    }
}
